using System;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Xsl;
using System.IO;

namespace Core
{
	/// <summary>
	/// XML-related utility class.
	/// </summary>
	public class XMLUtil
	{
		public static XmlDocument SerializeObjectModel (object objectToSerialize, Type typeOfObjectToSerialize)
		{
			XmlDocument retVal = new XmlDocument();
			string xml = string.Empty;
			XmlSerializer ser = new XmlSerializer(typeOfObjectToSerialize);
			using (StringWriter sw = new StringWriter())
			{
				ser.Serialize (sw, objectToSerialize);
				xml = sw.GetStringBuilder().ToString();
			}
			if (xml.Length > 0)
				retVal.LoadXml (xml);
			return retVal;
		}

		public static string TransformXml(XmlDocument docXml, XmlDocument docXsl, XsltArgumentList args, System.Security.Policy.Evidence evidence)
		{
			string retVal = string.Empty;
			XslTransform xslTrans = new XslTransform();
			xslTrans.Load (docXsl, new XmlUrlResolver(), evidence);
			using (StringWriter sw = new StringWriter())
			{
				xslTrans.Transform (docXml, args, sw, null);
				retVal = sw.GetStringBuilder().ToString();
			}
			return retVal;
		}
	}
}
